﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Restaurant.DAO
{

    public class KhoBD
    {
        private SqlConnection conn = KetNoiDB.TaoKetNoi();

        public DataTable LayKho()
        {
            try
            {

                conn.Open();
                string strCmd = "dbo.spLayKho";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return dbtab;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable LayKhoTheoMa(int ma)
        {
            try
            {

                conn.Open();
                string strCmd = "dbo.spLayKhoTheoMa";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maKho", ma));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return dbtab;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable LayKhoTheoNH(int ma)
        {
            try
            {

                conn.Open();
                string strCmd = "dbo.spLayKhoTheoNH";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maNH", ma));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return dbtab;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool ThemKho(string tenKho, int sucChua, int maNH)
        {
            try
            {

                conn.Open();
                string strCmd = "dbo.spThemKho";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@tenKho", tenKho));
                cmd.Parameters.Add(new SqlParameter("@SucChua", sucChua));
                cmd.Parameters.Add(new SqlParameter("@maNH", maNH));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool XoaKho(int maKho)
        {
            try
            {

                conn.Open();
                string strCmd = "dbo.spXoaKho";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maKho", maKho));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool SuaKho(int maKho, string tenKho, int sucChua, int maNH)
        {
            try
            {

                conn.Open();
                string strCmd = "dbo.spSuaKho";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maKho", maKho));
                cmd.Parameters.Add(new SqlParameter("@tenKho", tenKho));
                cmd.Parameters.Add(new SqlParameter("@sucChua", sucChua));
                cmd.Parameters.Add(new SqlParameter("@maNH", maNH));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
